package controller
{
	import away3d.animators.SkeletonAnimator;
	import away3d.animators.transitions.CrossfadeTransition;
	import away3d.entities.Mesh;
	import away3d.materials.ColorMaterial;
	import away3d.primitives.CubeGeometry;
	
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	import game.P2pWorld;
	
	import gameObject.GameObjectBase;
	import gameObject.Robot;
	import gameObject.resources3D.RobotResource;
	
	import tool.tweener.Tweener;

	public class FriendRobotController extends GameControllerBase
	{
		public var PosData:Array = new Array;
		
		private var _robot:Robot;
		private var _robotRes:RobotResource;
		
		private var _keepMoving:Boolean;
		private var timeOut:int;
		private var _moveTimer:Timer = new Timer(1000/60);
		
		public var tx:Number;
		public var ty:Number;
		
		private var vx:Number=0;
		private var vy:Number=0;
		
		private var tarX:Number;
		private var tarY:Number;
		
		public var t:Number;
		
		private var animator:SkeletonAnimator;
		private var transition:CrossfadeTransition = new CrossfadeTransition(0.2);
		/////////////////////////////////////////////////////////
		public function FriendRobotController(){}
		
		override public function setup(target:GameObjectBase):void
		{
			super.setup(target);
			
			_robot = target as Robot;
			_robotRes = _robot.resource as RobotResource;
			
			animator = _robotRes.legMesh.animator as SkeletonAnimator;
			
			
			animator.play("wait",transition);
			P2pWorld.robotControllers[_robot.uid] = this;
		}
		
		public function gotPosData():void
		{
			_keepMoving = true;
			
			if(animator.activeAnimationName == "wait")
			{
				animator.play("run",transition);
			}
			
			tarX = PosData[0] + 50*(Math.cos(PosData[2])*_robot.speed);
			tarY = PosData[1] - 50*(Math.sin(PosData[2])*_robot.speed);
			
			move();
		}
		
		public function tryStop():void
		{
			_keepMoving = false;
			
			//15是250ms后玩家没有改变移动方向将会到达的坐标(12居然更合适..额...)
			tarX = PosData[0] + 12*(Math.cos(PosData[2])*_robot.speed);
			tarY = PosData[1] - 12*(Math.sin(PosData[2])*_robot.speed);
			
			move();
		}
		
		private function move():void
		{
			//求出现在坐标与目标点间的距离
			var dx:Number = tarX - _robot.x;
			var dy:Number = _robot.z - tarY;
			var dis:Number = Math.sqrt(dx * dx + dy * dy);
			var angle:Number = Math.atan2(dy, dx);
			_robot.root.rotationY = angle*180/Math.PI-90;
			
			var time:Number = dis/_robot.speed;
			vx = 0.8*dx/time;
			vy = 0.8*dy/time;
			
			time *=  1/60;
			
			_moveTimer.removeEventListener(TimerEvent.TIMER,keepMoving);
			_moveTimer.stop();
			
			tx = _robot.x;
			ty = _robot.z;
			
			t = 0;
			Tweener.removeTweens(this);
			Tweener.addTween(this,
			{
				t:1,
				time:time,
				onUpdate:keepMoving,
				onComplete:doComplete,
				transition:"linear"
			});
		}
		
		private function keepMoving(e:TimerEvent = null):void
		{
			tx+=vx;
			ty-=vy;
			
			updatePos();
		}
		
		private function updatePos():void
		{
			_robot.hitArea.x = CollisionWorld.mapScaleX*(CollisionWorld.hitStartX+tx) - _robot.hitArea.radius;
			_robot.hitArea.y = CollisionWorld.mapScaleY*(CollisionWorld.hitStartY-_robot.z) - 2*_robot.hitArea.radius;
			if(CollisionWorld.roleHitTestBuilding(_robot) == false)_robot.x = tx;
			else
			{
				tx = _robot.x;
				_robot.hitArea.x = CollisionWorld.mapScaleX*(CollisionWorld.hitStartX+_robot.x) - _robot.hitArea.radius;
			}
			
			_robot.hitArea.y = CollisionWorld.mapScaleY*(CollisionWorld.hitStartY-ty) -2*_robot.hitArea.radius;
			_robot.hitArea.x = CollisionWorld.mapScaleX*(CollisionWorld.hitStartX+_robot.x)  -_robot.hitArea.radius;
			if(CollisionWorld.roleHitTestBuilding(_robot) == false)_robot.z = ty;
			else
			{
				ty = _robot.z;
				_robot.hitArea.y = CollisionWorld.mapScaleY*(CollisionWorld.hitStartY-_robot.z) -2*_robot.hitArea.radius;
			}
		}
		
		private function doComplete():void
		{
			_robot.root.rotationY = PosData[2]*180/Math.PI-90;
			
			if(_keepMoving)
			{
				vx = (Math.cos(PosData[2])*_robot.speed);
				vy = (Math.sin(PosData[2])*_robot.speed);
				
				//keepMoving();
				_moveTimer.addEventListener(TimerEvent.TIMER,keepMoving);
				_moveTimer.start();
			}else 
			{
				animator.play("wait",transition);
				
				_moveTimer.removeEventListener(TimerEvent.TIMER,keepMoving);
				_moveTimer.stop();
			}
		}
		
		
		
	}
}